package com.zzyl.nursing.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.zzyl.nursing.mapper.HealthAssessmentMapper;
import com.zzyl.oss.AliyunOSSOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author byZhao
 * @create 2025/6/28 12:18
 * @description 1、 AliyunOSSOperator 提供一个根据文件名称清理文件方法 （抄阿里手册）
 * 2、 从数据库读取健康评估表 读取 physical_report_url  截取到文件名
 * 3、 获取osss所有文件名称列表 (读阿里手册)和 数据库中physical_report_url 比对 存在则保留 不存在 调用1
 */


@SuppressWarnings("all")
@Component
public class CleanFileTask {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @Autowired
    private HealthAssessmentMapper healthAssessmentMapper;


    //@Scheduled(cron = "0 0 2 * * ?")
    public void clean() {
        // 获取当前日期
        LocalDateTime now = LocalDateTime.now();
        // 获取当前日期前一天
        LocalDateTime beforeDay = now.minusDays(1);
        //TODO 测试
       // LocalDateTime beforeDay = now;
        // 获取指定日期的开始时间
        LocalDateTime begin = LocalDateTimeUtil.beginOfDay(beforeDay);
        // 获取指定日期的结束时间
        LocalDateTime end = LocalDateTimeUtil.endOfDay(beforeDay);
        // 1. 获取阿里云服务器中指定日期前缀的目录所有文件
        // 前缀的处理
        String prefix = LocalDateTimeUtil.format(beforeDay, "yyyy/MM/dd");
        List<String> fileNameList = aliyunOSSOperator.getFileNameFromOss(prefix);
        Set<String> ossSet = list2set(fileNameList);
        // 2. 获取数据库中指定日期范围的 文件名称列表
        List<String> reportURL = healthAssessmentMapper.getReportURL(begin, end);
        Set<String> dbSet = handleURL(reportURL);
        // 3. 用我们oss集合 和 db集合 求单差集
        Collection<String> subtract = CollUtil.subtract(ossSet, dbSet);
        // 4. 调用阿里云oss删除
        List<String> urls = new ArrayList<String>(subtract);
        aliyunOSSOperator.delBatch(urls);
    }

    /**
     *  将数据库中url https://itheima-89.oss-cn-hangzhou.aliyuncs.com/2025/06/26/126afb54-1aea-4d06-87c0-b617b2eb7fcf.pdf
     *  处理成oss 需要 2025/06/26/126afb54-1aea-4d06-87c0-b617b2eb7fcf.pdf
     * @param urlList
     * @return
     */
     public Set<String> handleURL(List<String> urlList){
         Set<String> sets = new HashSet<>();
         String regex ="(https://itheima-89.oss-cn-hangzhou.aliyuncs.com/)(.+)";
         Pattern  pattern = Pattern.compile(regex);
         for (String s : urlList) {
             Matcher matcher = pattern.matcher(s);
             if (matcher.find()) {
                 String ossURL = matcher.group(2);
                 sets.add(ossURL);
             }
         }
        return sets;
     }

    /**
     *  封装一个工具类将list 转成set
     * @param list
     * @return
     */
    private Set<String>  list2set(List<String> list){
        Set<String> set = new HashSet<String>();
        for (String s : list) {
            set.add(s);
        }
        return set;
    }

}
